#include "DB.h"
#include "DBPageManager.h"
#include "DBIndexManager.h"
#include "DBError.h"

/*
int main()
{
	col colArr[3] = {	{"Gentoo", sizeof(int), DATA_INT},
						{"Roar", 14, DATA_CHAR},
						{"try", 23, DATA_VCHAR}
					};
	col* p = colArr;
	char* tableName = "TEST";
	Create(tableName, p, 3);
	return 0;
}*/


int Create(const char* tableName, col* colArr, int count)
{
	int i = 0;
	FILE* fp;
        int fileid;
        int pageid;
        int newpageid;
        int error;
        DB_PAGE* buf = NULL;
	unsigned long recordCount = 0;
	int colCount = count;
	col* readPointer = malloc(sizeof(col));
        char* fileName = malloc(50);
        strcpy(fileName, tableName);
        strcat(fileName, ".dbf");
        
        //printf("table name is %s.\n\n", fileName); 
	fp = fopen(fileName, "wb+");
	// write into file the recordNumber of the table
	// it's obvious that now is zero
	fwrite(&recordCount, sizeof(unsigned long), 1, fp);
	// write into file how many columns in the table
	fwrite(&colCount, sizeof(int), 1, fp);
	for (i = 0;i < count;i++, colArr++)
	{
		//printf("the col name is %s.\n", colArr->name);
		//printf("the col size is %d.\n", colArr->size);
		//printf("the col type is %d.\n", colArr->type);
		fwrite(colArr, sizeof(col), 1, fp);
		//printf("\n");
	}
	fclose(fp);
        

        // create database file for the table
        if((error = DBCreateFile(tableName))!=DB_OK)
	{
	    DBPrintError(error);
            return -1;
	}
        // open the file
        fileid = DBOpenFile(tableName);
        if (fileid < 0)
        {
            DBPrintError(error);
            return -1;
        }
        // allocate the first page
        error = DBAllocPage(fileid, &pageid, &buf);
        if (error < 0)
        {
            DBPrintError(error);
            return -1;
        }
		DBPageUsed(fileid, pageid);
		buf->pgheader.slotCnt = 0;
		DBPageRelease(fileid,pageid, TRUE);

        DBDisposePage(fileid, pageid);
        DBCloseFile(fileid);

        
        return 0;
        /*
        fileid = DBOpenFile(tableName);
        if (fileid < 0)
        {
            DBPrintError(error);
        }
        error = DBGetFirstPage(fileid, &newpageid, &buf);
        if (error < 0)
        {
            DBPrintError(error);
        }
        printf("the page get is : %d.\n", newpageid);
        printf("the slot count read is %d.\n", buf.pgheader.slotCnt);
        DBCloseFile(fileid);*/


	/*
	printf("size of unsigned long is : %d\n", sizeof(unsigned long));
	printf("size of char is : %d\n", sizeof(char));

	printf("The following is written content : \n\n");
	
	fp = fopen("test.dbf", "rb");
	fread(&recordCount, sizeof(unsigned long), 1, fp);
	printf("recordCount is : %d.\n", recordCount);
	fread(&colCount, sizeof(int), 1, fp);
	printf("colCount is : %d.\n", colCount);
	i = 0;
	for (i = 0;i < count;i++)
	{
		fread(readPointer, sizeof(col), 1, fp);
		printf("the col name is %s.\n", readPointer->name);
		printf("the col size is %d.\n", readPointer->size);
		printf("the col type is %d.\n", readPointer->type);
		printf("\n");
	}
	fclose(fp);

	colCount = 100;
	fp = fopen("test.dbf", "wb");
	fseek(fp, 4, 0);
	fwrite(&colCount, sizeof(int), 1, fp);
	fclose(fp);
	
	fp = fopen("test.dbf", "rb");
	fread(&recordCount, sizeof(unsigned long), 1, fp);
	printf("recordCount is : %d.\n", recordCount);
	fread(&colCount, sizeof(int), 1, fp);
	printf("colCount is : %d.\n", colCount);
	fclose(fp);
	*/
}


